home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_128 / mrbackup / compress.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  18KB  |  790 lines

  1. /* MRBackup: File Compression/Decompression Routines
  2.  * Filename:    Compress.c
  3.  * History:        (most recent change first)
  4.  *
  5.  * 09/19/87 -MRR- Fixed bugs in decompression routine, introduced by
  6.  *                new buffering scheme.
  7.  *
  8.  * 09/04/87 -MRR- This package now uses MRBackup's buffer to hold the
  9.  *                compressed data and uses AmigaDOS I/O.
  10.  */
  11.  
  12. /* 
  13.  * Compress.c - data compression/decompression routines.
  14.  * This is an adaptation of the public domain Un*x compress v4.0.
  15.  */
  16.  
  17. #include "MRBackup.h"
  18.  
  19. #define    min(a,b)    ((a>b) ? b : a)
  20. #define INBUFSIZE    4L*1024L        /* input buffer size */
  21.  
  22. #ifdef DEBUG
  23. #define DBG(x) x
  24. #else
  25. #define DBG(x)
  26. #endif
  27.  
  28. extern int errno;
  29.  
  30. /*
  31.  * Set USERMEM to the maximum amount of physical user memory available
  32.  * in bytes.  USERMEM is used to determine the maximum BITS that can be used
  33.  * for compression.
  34.  *
  35.  * SACREDMEM is the amount of physical memory saved for others; compress
  36.  * will hog the rest.
  37.  */
  38. #ifndef SACREDMEM
  39. #define SACREDMEM    0
  40. #endif
  41.  
  42. #ifndef USERMEM
  43. # define USERMEM     450000            /* default user memory */
  44. #endif
  45.  
  46. # define MAXFILES       100            /* arbitrary maximum - see note below */
  47. # define BITS        12                /* > 12 crashes system (?) */
  48. # undef USERMEM
  49. long    filesize();
  50. char   *scdir();
  51.  
  52. #ifdef USERMEM
  53. # if USERMEM >= (433484+SACREDMEM)
  54. #  define PBITS    16
  55. # else
  56. #  if USERMEM >= (229600+SACREDMEM)
  57. #   define PBITS    15
  58. #  else
  59. #   if USERMEM >= (127536+SACREDMEM)
  60. #    define PBITS    14
  61. #   else
  62. #    if USERMEM >= (73464+SACREDMEM)
  63. #     define PBITS    13
  64. #    else
  65. #     define PBITS    12
  66. #    endif
  67. #   endif
  68. #  endif
  69. # endif
  70. # undef USERMEM
  71. #endif                                /* USERMEM */
  72.  
  73. #ifdef PBITS                        /* Preferred BITS for this memory size */
  74. # ifndef BITS
  75. #  define BITS PBITS
  76. # endif BITS
  77. #endif                                /* PBITS */
  78.  
  79. #if BITS == 16
  80. # define HSIZE    69001L                /* 95% occupancy */
  81. #endif
  82. #if BITS == 15
  83. # define HSIZE    35023L                /* 94% occupancy */
  84. #endif
  85. #if BITS == 14
  86. # define HSIZE    18013L                /* 91% occupancy */
  87. #endif
  88. #if BITS == 13
  89. # define HSIZE    9001L                /* 91% occupancy */
  90. #endif
  91. #if BITS <= 12
  92. # define HSIZE    5003L                /* 80% occupancy */
  93. #endif
  94.  
  95. /*
  96.  * a code_int must be able to hold 2**BITS values of type int, and also -1
  97.  */
  98. #if BITS > 15
  99. typedef long int    code_int;
  100. #else
  101. typedef int     code_int;
  102. #endif
  103.  
  104. #ifdef SIGNED_COMPARE_SLOW
  105. typedef unsigned long int count_int;
  106. typedef unsigned short int count_short;
  107. #else
  108. typedef long int    count_int;
  109. #endif
  110.  
  111. #ifdef NO_UCHAR
  112. typedef char    char_type;
  113. #else
  114. typedef unsigned char   char_type;
  115. #endif                                /* UCHAR */
  116. char_type magic_header[]= {
  117.     "\037\235" 
  118. };                                    /* 1F 9D */
  119.  
  120. /* Defines for third byte of header */
  121. #define BIT_MASK    0x1f
  122. #define BLOCK_MASK    0x80
  123. /* Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
  124.    a fourth header byte (for expansion).
  125. */
  126. #define INIT_BITS 9                    /* initial number of bits/code */
  127.  
  128. /*
  129.  * compress.c - File compression ala IEEE Computer, June 1984.
  130.  *
  131.  * Authors:    
  132.  *        Spencer W. Thomas    (decvax!harpo!utah-cs!utah-gr!thomas)
  133.  *        Jim McKie            (decvax!mcvax!jim)
  134.  *        Steve Davies        (decvax!vax135!petsd!peora!srd)
  135.  *        Ken Turkowski        (decvax!decwrl!turtlevax!ken)
  136.  *        James A. Woods        (decvax!ihnp4!ames!jaw)
  137.  *        Joe Orost            (decvax!vax135!petsd!joe)
  138.  *        Mark R. Rinfret        (mods) (mark@unisec.USI.COM)
  139.  */
  140.  
  141. static unsigned endInput;            /* true => input file exhausted */
  142. static int n_bits;                    /* number of bits/code */
  143. static int maxbits = BITS;            /* user settable max # bits/code */
  144. static code_int maxcode;            /* maximum code, given n_bits */
  145. static code_int maxmaxcode = 1 << BITS;    /* NEVER generate this code */
  146. #define MAXCODE(n_bits)    ((1 << (n_bits)) - 1)
  147.  
  148. static count_int htab [HSIZE];
  149. static USHORT codetab [HSIZE];
  150. #define htabof(i)        htab[i]
  151. #define codetabof(i)    codetab[i]
  152.  
  153. static code_int hsize = HSIZE;        /* for dynamic table sizing */
  154. static count_int fsize;
  155.  
  156. static struct FileHandle *infile, *outfile;
  157. static char iname[256], oname[256];
  158. static UBYTE inbuf[INBUFSIZE], *inbufptr;
  159. static LONG inbufsize;
  160. static UBYTE *outbufptr;
  161.  
  162. /*
  163.  * To save much memory, we overlay the table used by compress() with those
  164.  * used by decompress().  The tab_prefix table is the same size and type
  165.  * as the codetab.  The tab_suffix table needs 2**BITS characters.  We
  166.  * get this from the beginning of htab.  The output stack uses the rest
  167.  * of htab, and contains characters.  There is plenty of room for any
  168.  * possible stack (stack used to be 8000 characters).
  169.  */
  170.  
  171. #define tab_prefixof(i)    codetabof(i)
  172. #define tab_suffixof(i)    ((char_type *)(htab))[i]
  173. #define de_stack        ((char_type *)&tab_suffixof(1<<BITS))
  174.  
  175. static code_int free_ent = 0;        /* first unused entry */
  176. static int status = 0;
  177.  
  178. code_int getcode();
  179.  
  180. static int nomagic = 0;                /* Use a 3-byte magic number header,
  181.                                        unless old file */
  182. static int zcat_flg = 0;            /* Write output on stdout, suppress
  183.                                        messages */
  184. static int quiet = 1;                /* don't tell me about compression */
  185.  
  186. /*
  187.  * block compression parameters -- after all codes are used up,
  188.  * and compression rate changes, start over.
  189.  */
  190. static int block_compress = BLOCK_MASK;
  191. static int clear_flg = 0;
  192. static long int ratio = 0;
  193. #define CHECK_GAP 10000                /* ratio check interval */
  194. static count_int checkpoint = CHECK_GAP;
  195. /*
  196.  * the next two codes should not be changed lightly, as they must not
  197.  * lie within the contiguous general code space.
  198.  */
  199. #define FIRST    257                    /* first free entry */
  200. #define    CLEAR    256                    /* table clear output code */
  201.  
  202. static int force = 0;
  203. static char ofname [100];
  204. #ifdef DEBUG
  205. static int verbose = 0;
  206. #endif                                /* DEBUG */
  207. int     (*bgnd_flag)();
  208.  
  209. static int do_decomp = 0;
  210. static int offset;
  211. static LONG in_count;                /* length of input */
  212. static LONG bytes_out;                /* length of compressed output */
  213. static LONG out_count;                /* # of codes output (for debugging) */
  214. static LONG outbufcount;            /* # bytes in output buffer */
  215.  
  216. /*****************************************************************
  217.  *
  218.  * Algorithm from "A Technique for High Performance Data Compression",
  219.  * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
  220.  *
  221.  * Algorithm:
  222.  * Modified Lempel-Ziv method (LZW).  Basically finds common
  223.  * substrings and replaces them with a variable size code.  This is
  224.  * deterministic, and can be done on the fly.  Thus, the decompression
  225.  * procedure needs no input table, but tracks the way the table was built.
  226.  */
  227.  
  228. static void
  229. comp_init()
  230. {
  231.     if(maxbits < INIT_BITS)
  232.         maxbits = INIT_BITS;
  233.     if (maxbits > BITS)
  234.         maxbits = BITS;
  235.     maxmaxcode = 1 << maxbits;
  236.     outbufcount = 0;
  237.     outbufptr = buffer;
  238. }
  239.  
  240.  
  241. /*
  242.  * compress "from" to "to"
  243.  *
  244.  * Algorithm:  use open addressing double hashing (no chaining) on the 
  245.  * prefix code / next character combination.  We do a variant of Knuth's
  246.  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
  247.  * secondary probe.  Here, the modular division first probe is gives way
  248.  * to a faster exclusive-or manipulation.  Also do block compression with
  249.  * an adaptive reset, whereby the code table is cleared when the compression
  250.  * ratio decreases, but after the table fills.  The variable-length output
  251.  * codes are re-sized at this point, and a special CLEAR code is generated
  252.  * for the decompressor.  Late addition:  construct the table according to
  253.  * file size for noticeable speed improvement on small files.  Please direct
  254.  * questions about this implementation to ames!jaw.
  255.  */
  256.  
  257. int 
  258. compress(from, to)
  259.     char *from, *to;
  260. {
  261.     register long   fcode;
  262.     register code_int i = 0;
  263.     register int    c;
  264.     register code_int ent;
  265.     register int    disp;
  266.     register code_int hsize_reg;
  267.     register int    hshift;
  268.     char *s;
  269.     char *err_msg[256];
  270.  
  271.     comp_init();
  272.     inbufsize = 0;
  273.     endInput = false;
  274.  
  275.     /* 
  276.      * tune hash table size for small files -- ad hoc,
  277.      * but the sizes match earlier #defines, which
  278.      * serve as upper bounds on the number of output codes. 
  279.      */
  280.         hsize = HSIZE;
  281.         if (fsize < (1L << 12))
  282.             hsize = min (5003L,HSIZE );
  283.         else 
  284.             if (fsize < (1L << 13))
  285.                 hsize = min (9001L,HSIZE );
  286.             else 
  287.                 if (fsize < (1L << 14))
  288.                     hsize = min (18013L,HSIZE );
  289.                 else 
  290.                     if (fsize < (1L << 15))
  291.                         hsize = min (35023L,HSIZE );
  292.                     else 
  293.                         if (fsize < 47000L )
  294.                             hsize = min (50021L,HSIZE );
  295.  
  296.     if (!(infile = Open(from, MODE_OLDFILE))) {
  297.         return IoErr();
  298.     }
  299.  
  300.     if (!(outfile = Open(to, MODE_NEWFILE))) {
  301.         status = IoErr();
  302.         Close(infile);
  303.         return status;
  304.     }
  305.  
  306.     if (nomagic == 0){
  307.         putcbuf(magic_header[0]);
  308.         putcbuf(magic_header[1]);
  309.         putcbuf((char)(maxbits | block_compress));
  310.         if(status) {
  311. cleanup:
  312.             Close(infile);
  313.             Close(outfile);
  314.             return status;
  315.         }
  316.     }
  317.  
  318.     offset = 0;
  319.     bytes_out = 3;                    /* includes 3-byte header mojo */
  320.     out_count = 0;
  321.     clear_flg = 0;
  322.     ratio = 0L;
  323.     in_count = 1;
  324.     checkpoint = CHECK_GAP;
  325.     maxcode = MAXCODE(n_bits = INIT_BITS);
  326.     free_ent = ((block_compress)?FIRST :256 );
  327.  
  328.     ent = ReadChar();
  329.  
  330.     hshift = 0;
  331.     for (fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
  332.         hshift++;
  333.     hshift = 8 - hshift;            /* set hash code range bound */
  334.  
  335.     hsize_reg = hsize;
  336.     cl_hash((count_int)hsize_reg);    /* clear hash table */
  337.  
  338.     /*while ((c = getc(infile))!= EOF ){*/
  339.     while ( ( c = ReadChar() ) != EOF) {
  340.         in_count++;
  341.         fcode = (long)(((long)c << maxbits)+ ent);
  342.         i = ((c << hshift)^ ent);    /* xor hashing */
  343.  
  344.         if (htabof (i)== fcode ){
  345.             ent = codetabof (i);
  346.             continue;
  347.         }
  348.         else 
  349.             if ((long)htabof (i)< 0 )/* empty slot */
  350.                 goto nomatch;
  351.         disp = hsize_reg - i;        /* secondary hash (after G. Knott) */
  352.         if (i == 0 )
  353.             disp = 1;
  354. probe: 
  355.         if ((i -= disp)< 0 )
  356.             i += hsize_reg;
  357.  
  358.         if (htabof (i)== fcode ){
  359.             ent = codetabof (i);
  360.             continue;
  361.         }
  362.         if ((long)htabof (i)> 0 )
  363.             goto probe;
  364. nomatch: 
  365.         output ((code_int)ent );
  366.         out_count++;
  367.         ent = c;
  368.         if (free_ent < maxmaxcode ){
  369.             codetabof (i)= free_ent++;/* code -> hashtable */
  370.             htabof (i)= fcode;
  371.         }
  372.         else 
  373.             if ((count_int)in_count >= checkpoint && block_compress )
  374.                 cl_block ();
  375.     }
  376.  /* 
  377.   * Put out the final code.
  378.   */
  379.     output((code_int)ent );
  380.     out_count++;
  381.     output((code_int)-1 );
  382.  
  383.  /* 
  384.   * Print out stats on stderr
  385.   */
  386.     if(zcat_flg == 0 && !quiet){
  387. #ifdef DEBUG
  388.         fprintf(stderr,
  389.             "%ld chars in, %ld codes (%ld bytes) out, compression factor: ",
  390.             in_count,out_count,bytes_out );
  391.         prratio(stderr,in_count,bytes_out );
  392.         fprintf(stderr,"\n");
  393.         fprintf(stderr,"\tCompression as in compact: " );
  394.         prratio(stderr,in_count-bytes_out,in_count );
  395.         fprintf(stderr,"\n");
  396.         fprintf(stderr,"\tLargest code (of last block) was %d (%d bits)\n",
  397.                 free_ent - 1,n_bits );
  398. #endif                                /* DEBUG */
  399.     }
  400.     flushbuf();                        /* dump the last block */
  401.     if(bytes_out > in_count)        /* exit(2) if no savings */
  402.         status = 2;
  403.     goto cleanup;
  404. }
  405.  
  406. /*****************************************************************
  407.  * TAG( output )
  408.  *
  409.  * Output the given code.
  410.  * Inputs:
  411.  *     code:    A n_bits-bit integer.  If == -1, then EOF.  This assumes
  412.  *        that n_bits =< (long)wordsize - 1.
  413.  * Outputs:
  414.  *     Outputs code to the file.
  415.  * Assumptions:
  416.  *    Chars are 8 bits long.
  417.  * Algorithm:
  418.  *     Maintain a BITS character long buffer (so that 8 codes will
  419.  * fit in it exactly).  Use the VAX insv instruction to insert each
  420.  * code in turn.  When the buffer fills up empty it and start over.
  421.  */
  422.  
  423. static char     buf[BITS];
  424.  
  425. char_type lmask[9]= {
  426.     0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00
  427. };
  428. char_type rmask[9]= {
  429.     0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff
  430. };
  431.  
  432. output(code)
  433.     code_int code;
  434. {
  435. #ifdef DEBUG
  436.     static int col = 0;
  437. #endif                                /* DEBUG */
  438.  
  439.     register int r_off = offset, bits = n_bits;
  440.     register char * bp = buf;
  441.     int count;
  442.  
  443. #ifdef DEBUG
  444.     if (verbose )
  445.         fprintf(stderr,"%5d%c",code,
  446.                 (col+=6)>= 74 ?(col = 0,'\n'):' ' );
  447. #endif                                /* DEBUG */
  448.     if (code >= 0 ){
  449. /* 
  450.  * byte/bit numbering on the VAX is simulated by the following code
  451.  */
  452.     /* 
  453.      * Get to the first byte.
  454.      */
  455.         bp += (r_off >> 3);
  456.         r_off &= 7;
  457.     /* 
  458.      * Since code is always >= 8 bits, only need to mask the first
  459.      * hunk on the left.
  460.      */
  461.         *bp = (*bp & rmask[r_off])| (code << r_off)& lmask[r_off];
  462.         bp++;
  463.         bits -= (8 - r_off);
  464.         code >>= 8 - r_off;
  465.  
  466.     /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  467.  
  468.         if (bits >= 8 ){
  469.             *bp++= code;
  470.             code >>= 8;
  471.             bits -= 8;
  472.         }
  473.     /* Last bits. */
  474.         if(bits)
  475.             *bp = code;
  476.         offset += n_bits;
  477.         if (offset == (n_bits << 3)){
  478.             bp = buf;
  479.             bits = n_bits;
  480.             bytes_out += bits;
  481.             do
  482.                 putcbuf(*bp++);
  483.             while(--bits);
  484.             offset = 0;
  485.         }
  486.  
  487.     /* 
  488.      * If the next entry is going to be too big for the code size,
  489.      * then increase it, if possible.
  490.      */
  491.         if (free_ent > maxcode || (clear_flg > 0)){
  492.         /* 
  493.          * Write the whole buffer, because the input side won't
  494.          * discover the size increase until after it has read it.
  495.          */
  496.             if (offset > 0 ){
  497.                 putmcbuf(buf, n_bits);
  498.                 if (status) return 1;
  499.                 bytes_out += n_bits;
  500.             }
  501.             offset = 0;
  502.  
  503.             if (clear_flg ){
  504.                 maxcode = MAXCODE (n_bits = INIT_BITS);
  505.                 clear_flg = 0;
  506.             }
  507.             else {
  508.                 n_bits++;
  509.                 if (n_bits == maxbits )
  510.                     maxcode = maxmaxcode;
  511.                 else
  512.                     maxcode = MAXCODE(n_bits);
  513.             }
  514. #ifdef DEBUG
  515.             if (debug ){
  516.                 fprintf(stderr,"\nChange to %d bits\n",n_bits );
  517.                 col = 0;
  518.             }
  519. #endif                                /* DEBUG */
  520.         }
  521.     }
  522.     else {
  523.     /* 
  524.      * At EOF, write the rest of the buffer.
  525.      */
  526.          coun= MAXCODE (n_bits = INIT_BITS);
  527.             clear_flg = 0;
  528.         }
  529.         size = 0;
  530.         while (size < n_bits) {
  531.             if ((c = ReadChar()) == EOF || status ) break;
  532.             buf[size++] = c;
  533.         }
  534.         if (size == 0  || status) {
  535.             return EOF;                /* end of file */
  536.         }
  537.  
  538.         offset = 0;
  539.  
  540.         /* Round size down to integral number of codes */
  541.  
  542.         size = (size << 3)- (n_bits - 1);
  543.     }
  544.     r_off = offset;
  545.     bits = n_bits;
  546.  /* 
  547.   * Get to the first byte.
  548.   */
  549.     bp += (r_off >> 3);
  550.     r_off &= 7;
  551.  
  552.  /* Get first part (low order bits) */
  553.  
  554. #ifdef NO_UCHAR
  555.     code = ((*bp++ >> r_off)& rmask[8 - r_off]) & 0xff;
  556. #else
  557.     code = (*bp++ >> r_off);
  558. #endif                                /* NO_UCHAR */
  559.     bits -= (8 - r_off);
  560.     r_off = 8 - r_off;                /* now, offset into code word */
  561.  
  562.     /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  563.  
  564.     if (bits >= 8 ){
  565. #ifdef NO_UCHAR
  566.         code |= (*bp++ & 0xff) << r_off;
  567. #else
  568.         code |= *bp++ << r_off;
  569. #endif                                /* NO_UCHAR */
  570.         r_off += 8;
  571.         bits -= 8;
  572.     }
  573.  /* high order bits. */
  574.     code |= (*bp & rmask[bits]) << r_off;
  575.     offset += n_bits;
  576.  
  577.     return code;
  578. }
  579.  
  580. #ifndef AZTEC_C
  581. char *
  582. rindex(s,c)
  583.     register char *s,c;
  584. {
  585.     char   *p;
  586.     for (p = NULL;*s;s++)
  587.         if (*s == c)
  588.             p = s;
  589.     return(p);
  590. }
  591. #endif
  592.  
  593. /*
  594.  * This routine returns 1 if we are running in the foreground and stderr
  595.  * is a tty.
  596.  */
  597. foreground()
  598. {
  599.      /* I don't know how to test for background, yet. */
  600.     return (isatty(2));
  601. }
  602.  
  603. onintr()
  604. {
  605.     unlink ( ofname );
  606.     exit (1 );
  607. }
  608.  
  609. /* wild pointer -- assume bad input */
  610.  
  611. oops ()
  612. {                            
  613.     if ( do_decomp == 1 )
  614.         fprintf (stderr,"uncompress: corrupt input\n" );
  615.     unlink ( ofname );
  616.     exit ( 1 );
  617. }
  618.  
  619. /* table clear for block compress */
  620.  
  621. cl_block ()
  622. {                        
  623.     register long int rat;
  624.  
  625.     checkpoint = in_count + CHECK_GAP;
  626. #ifdef DEBUG
  627.     if (debug ){
  628.         fprintf (stderr,"count: %ld, ratio: ",in_count );
  629.         prratio (stderr,in_count,bytes_out );
  630.         fprintf (stderr,"\n");
  631.     }
  632. #endif                                /* DEBUG */
  633.  
  634.     if(in_count > 0x007fffff){        /* shift will overflow */
  635.         rat = bytes_out >> 8;
  636.         if(rat == 0){                /* Don't divide by zero */
  637.             rat = 0x7fffffff;
  638.         }
  639.         else {
  640.             rat = in_count / rat;
  641.         }
  642.     }
  643.     else {
  644.         rat = (in_count << 8) / bytes_out; /* 8 fractional bits */
  645.     }
  646.     if (rat > ratio ){
  647.         ratio = rat;
  648.     }
  649.     else {
  650.         ratio = 0;
  651. #ifdef DEBUG
  652.         if(verbose)
  653.             dump_tab();                /* dump string table */
  654. #endif
  655.         cl_hash ((count_int)hsize );
  656.         free_ent = FIRST;
  657.         clear_flg = 1;
  658.         output ((code_int)CLEAR );
  659. #ifdef DEBUG
  660.         if(debug)
  661.             fprintf (stderr,"clear\n" );
  662. #endif                                /* DEBUG */
  663.     }
  664. }
  665.  
  666. cl_hash(hsize)                        /* reset code table */
  667. register count_int hsize;
  668. {
  669.     register count_int *htab_p = htab+hsize;
  670.     register long i;
  671.     register long m1 = -1;
  672.  
  673.     i = hsize - 16;
  674.     do {                            /* might use Sys V memset(3) here */
  675.         *(htab_p-16)= m1;
  676.         *(htab_p-15)= m1;
  677.         *(htab_p-14)= m1;
  678.         *(htab_p-13)= m1;
  679.         *(htab_p-12)= m1;
  680.         *(htab_p-11)= m1;
  681.         *(htab_p-10)= m1;
  682.         *(htab_p-9)= m1;
  683.         *(htab_p-8)= m1;
  684.         *(htab_p-7)= m1;
  685.         *(htab_p-6)= m1;
  686.         *(htab_p-5)= m1;
  687.         *(htab_p-4)= m1;
  688.         *(htab_p-3)= m1;
  689.         *(htab_p-2)= m1;
  690.         *(htab_p-1)= m1;
  691.         htab_p -= 16;
  692.     } while ((i -= 16) >= 0);
  693.  
  694.     for (i += 16; i > 0; i--)
  695.         *--htab_p = m1;
  696. }
  697. #ifdef DEBUG
  698. prratio(stream,num,den)
  699. FILE *stream;
  700. long int    num,den;
  701. {
  702.     register int    q;                /* Doesn't need to be long */
  703.  
  704.     if(num > 214748L){                /* 2147483647/10000 */
  705.         q = num / (den / 10000L);
  706.     }
  707.     else {
  708.         q = (10000L*num)/ den;        /* Long calculations, though */
  709.     }
  710.     if (q < 0){
  711.         putc('-',stream);
  712.         q = -q;
  713.     }
  714.     fprintf(stream,"%d.%02d%%",q / 100,q % 100);
  715. }
  716. #endif
  717.  
  718. /* Flush the output buffer. */
  719.  
  720. flushbuf()
  721. {
  722.     if (Write(outfile, buffer, outbufcount) != outbufcount) {
  723.         status = IoErr();
  724.     }
  725.     outbufcount = 0;
  726.     outbufptr = buffer;
  727. }
  728.  
  729. /* Put a character into the output buffer.  If the buffer is full,
  730.  * output the buffer, then reset its count to zero.
  731.  * Called with:
  732.  *        c:        character to be output
  733.  * Returns:
  734.  *        status code (0 => OK)
  735.  */
  736.  
  737. int
  738. putcbuf(c)
  739. {
  740.     if (outbufcount >= bufsize)     flushbuf();
  741.     *outbufptr++ = c;
  742.     ++outbufcount;
  743.     return status;
  744. }
  745.  
  746. /* Put multiple characters in the output buffer.
  747.  * Called with:
  748.  *        buf:        address of character array
  749.  *        count:        number of characters to output
  750.  * Returns:
  751.  *        status 
  752.  */
  753.  
  754. putmcbuf(buf, count)
  755.     char *buf; int count;
  756. {
  757.     for (; count; --count)     if ( putcbuf( *buf++ ) ) break;
  758.     return status;
  759. }
  760.  
  761. /* Read 1 character from the input file.
  762.  * Returns:
  763.  *   character code or -1 (EOF)
  764.  */
  765.  
  766. int
  767. ReadChar()
  768. {
  769. again:
  770.     if (inbufsize == 0) {
  771.         if (endInput)
  772.             return EOF;
  773.  
  774.         if ((inbufsize = Read(infile, inbuf, INBUFSIZE)) == -1L) {
  775.             status = IoErr();
  776.             return EOF;
  777.         }
  778.  
  779.         inbufptr = inbuf;                /* reset buffer pointer */
  780.  
  781.         if (inbufsize < INBUFSIZE) {
  782.             endInput = true;
  783.             goto again;
  784.         }
  785.     }
  786.     ++in_count;
  787.     --inbufsize;
  788.     return (*inbufptr++);
  789. }
  790.